//
//  MessageCenterViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/17.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "MessageCenterNaviView.h"
#import "NormalNaviView.h"

#import "InteractionMsgViewController.h"
#import "SystemNotiSegViewController.h"
#import "GuestResponseSegViewController.h"
#import "TT_ConversationListController.h"
#import "OrderMsgViewController.h"
#import "PlaformMsgViewController.h"

#import "TT_ConversationListController.h"
#import "TT_ConversationDetailViewController.h"

#import "MessageCenterViewController.h"

@interface MessageCenterViewController ()

@end

@implementation MessageCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
}

- (void)initView {
    
    self.title = @"消息";
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitleColor:UIColorHex(0x666666) forState:UIControlStateNormal];
    [rightBtn setTitle:@"系统消息" forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(goPlatformMsgList) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    rightBtn.frame = CGRectMake(0,0,66,30);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];

    self.navigationItem.rightBarButtonItems =@[rightItem];
    

    [[TT_MessageManager manager] getSysUnReadMessage:^(int unReadMsgCount) {}];
    [RACObserve([TT_MessageManager manager], SysUnReadCount) subscribeNext:^(id x) {
        if ([TT_MessageManager manager].SysUnReadCount > 0) {
            rightBtn.badgeValue = [NSString stringWithFormat:@"%d",[TT_MessageManager manager].SysUnReadCount];
            rightBtn.badgeBGColor = [UIColor redColor];
        } else {
            rightBtn.badgeValue = @"";
            rightBtn.badgeBGColor = [UIColor clearColor];
        }
    }];
    
    if ([TTUserModel sharedObject].token.length > 0) {
        //已登录
        [self initImUI];

    } else {
        //未登录
        [[TTNoResultManager shareManager] showNoResultWithStateInView:self.view errorCode:kTTViewState_UnLogin btnTitle:@"登录" didPressButton:^{
            
            [self openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {
                if (isLogin) {
                    [[TTNoResultManager shareManager] hideNoResultWithView:self.view];
                    
                    [SVProgressHUD showWithStatus:@"正在拉取聊天信息"];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [SVProgressHUD dismiss];
                        [self initImUI];
                    });
                }
            }];
        }];
    }
}

- (void)initImUI {
    
    TT_ConversationListController *conv = [[TT_ConversationListController alloc] init];
    [self addChildViewController:conv];
    [self.view addSubview:conv.view];
    
    [conv.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.mas_equalTo(0);
    }];
    
    [self.view setNeedsLayout];
}

- (void)goPlatformMsgList {
    PlaformMsgViewController *vc = [[PlaformMsgViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
